<?php
/**
 * Created by PhpStorm.
 * User: fanzhaogui
 * Date: 2021/4/8
 * Time: 11:20
 */

namespace App\HttpController;


use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Mysqli\Client;
use EasySwoole\Mysqli\Config;
use EasySwoole\Mysqli\QueryBuilder;


class TryBest extends Controller
{
    /**
     * @request TryBest/index
     */
    public function index()
    {

        # 使用mysqli来操作数据库
        $config = new Config([
            'host'     => '127.0.0.1',//防止报错,就不切换数据库了
            'port'     => '3306',
            'user'     => 'root',//数据库用户名
            'password' => '123456',//数据库密码
            'database' => 'test',//数据库
            'timeout'  => '5',
            'charset'  => 'utf8',
        ]);

        $client = new Client($config);

        // 构建sql
        // $client->queryBuilder()->get('user_list');
        // 执行sql
        // var_dump($client->execBuilder());


        # 基本查询
        // 一条数据，
        // $client->queryBuilder()->where('userId', 1)->getOne('user_list');
        // 执行sql
        // var_dump($client->execBuilder());
        // 多条数据
        // $client->queryBuilder()->where('userId', 0, '>')->get('user_list');

        # 添加数据
        # insert into
        // $client->queryBuilder()->insert('user', [
        //     'usename' => '000' . mt_rand(1000,9999),
        // ]);
        # replace into 需要唯一才行
        // $client->queryBuilder()->replace('user', [
        //     'usename' => '0009036'
        // ]);




        # 更新数据
        // where update
        // $client->queryBuilder()
        //     ->where('id', '1')
        //     ->update('user', [
        //         'usename' => 'update_name'
        //     ]);

        // limit update
        // $client->queryBuilder()
        //     ->update('user', [
        //         'usename' => 'limit_update_name'
        //     ], 5);

        // 快捷更新
        // $client->queryBuilder()
        //     ->where('usename', 'limit_update_name')
        //     ->update('user', [
        //         'age'    => QueryBuilder::inc(1),
        //         'number' => QueryBuilder::dec(3),
        //     ]);

        # 删除数据
        // limit delete
        // $client->queryBuilder()
        //     ->delete('user', 5);
        // where delete
        $client->queryBuilder()
            ->where('id', 20)
            ->delete('user');

        // 执行sql
        var_dump($client->execBuilder());

        // var_dump($client->mysqlClient()->insert_id);

        $this->response()->write('ojbk');
    }

    public function test()
    {
        $builder = new QueryBuilder();

        ##########GET

        //执行条件构造逻辑
        $builder->where('col1', 2)->get('my_table');

        //获取最后的查询参数
        // var_dump($builder->getLastQueryOptions());

        //获取子查询
        echo $builder->getSubQuery();

        //获取上次条件构造的预处理sql语句
        echo $builder->getLastPrepareQuery();
        // SELECT  * FROM whereGet WHERE  col1 = ?

        //获取上次条件构造的预处理sql语句所以需要的绑定参数
       var_dump($builder->getLastBindParams());
        //[2]

        //获取上次条件构造的sql语句
        echo $builder->getLastQuery();
        //SELECT  * FROM whereGet WHERE  col1 = 2

        // 获取最后插入的insert_id 使用客户端从swoole mysql获取
        // $client->mysqlClient()->insert_id

    }


}